package com.beyondsoft.mcms.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class DateUtil {
	
	public static final String DATA_FORMAT_COMMON = "yyyy-MM-dd";

	//add day
	public static String dateAdd(Date date, int count, String format) {
		SimpleDateFormat df = null;
		String dateStr = "";

		if (format != null && !"".equals(format))
			df = new SimpleDateFormat(format);
		else
			df = new SimpleDateFormat(DATA_FORMAT_COMMON);

		dateStr = df.format(new Date(date.getTime() + count * 24 * 60 * 60
				* 1000));
		return dateStr;
	}

	public static String dateAddMonth(Date date, int count, String format) {
		SimpleDateFormat df = null;
		String dateStr = "";

		if (format != null && !"".equals(format))
			df = new SimpleDateFormat(format);
		else
			df = new SimpleDateFormat(DATA_FORMAT_COMMON);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.MONTH, count);
		Date temp_date = c.getTime();
		dateStr = df.format(temp_date);
		return dateStr;
	}

	public static String getStartDate(Date date, int count, String format) {
		SimpleDateFormat df = null;
		String dateStr = "";

		if (format != null && !"".equals(format))
			df = new SimpleDateFormat(format);
		else
			df = new SimpleDateFormat(DATA_FORMAT_COMMON);
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.MONTH, count);
		c.add(c.DATE, 1);
		Date temp_date = c.getTime();
		dateStr = df.format(temp_date);
		return dateStr;
	}

	public static long getDays(String date1, String date2) {
		if (date1 == null || date1.equals(""))
			return 0;
		if (date2 == null || date2.equals(""))
			return 0;


		SimpleDateFormat myFormatter = new SimpleDateFormat(DATA_FORMAT_COMMON);
		java.util.Date date = null;
		java.util.Date mydate = null;
		try {
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
		} catch (Exception e) {
			return 0;
		}
		long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}

	public static long getDays(Date date1, Date date2) {
		if (date1 == null || date1.equals(""))
			return 0;
		if (date2 == null || date2.equals(""))
			return 0;
		long day = (date1.getTime() - date2.getTime());
		return day;
	}

	public static double RunTime(Date date) {
		if (null == date) {
			return 0.0;
		}
		long runTime = new Date().getTime() - date.getTime();
		return runTime;
	}

	public static double RunTime(long start) {
		return (System.currentTimeMillis() - start);
	}

	public static Date strParseDate(String strDate, String format) {
		SimpleDateFormat sf = null;
		if (format != null && !"".equals(format))
			sf = new SimpleDateFormat(format);
		else
			sf = new SimpleDateFormat(DATA_FORMAT_COMMON);
		try {
			return sf.parse(strDate);
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date strParseDate(String strDate) {
		return strParseDate(strDate, "yyyy-MM-dd HH:mm:ss");
	}

	public static String dateParseStr(Date date, String format) {
		if(null==date){
			return "";
		}
		SimpleDateFormat sf = null;
		if (format != null && !"".equals(format))
			sf = new SimpleDateFormat(format);
		else
			sf = new SimpleDateFormat(DATA_FORMAT_COMMON);

		return sf.format(date);
	}

	public static String dateParseStr(Date date) {
		String sf = DATA_FORMAT_COMMON;
		return dateParseStr(date, sf);
	}

	public static String dateParseStr(Date date, boolean checkDate) {
		String sf = DATA_FORMAT_COMMON;
		return dateParseStr(date, sf, checkDate);
	}
	
	public static String dateParseStr(Date date, String format, boolean checkDate) {
		if (checkDate == true && date == null)
			return "";
		return dateParseStr(date, format);
	}

	public static String calcTime(Date startDate, Date endDate) {
		if (startDate == null || endDate == null)
			return "";
		return String.valueOf((endDate.getTime() - startDate.getTime()));
	}

	public static String getChangedString(String str, Map hashMap,
			String patternStr) {
		Pattern pattern = Pattern.compile(patternStr);
		Matcher m = pattern.matcher(str);
		while (m.find()) {
			String group1 = m.group(1);
			String value = "";
			if (hashMap.containsKey(group1)) {
				value = (String) hashMap.get(group1);
			}
			str = str.replace(m.group(0), value);
		}
		return str;
	}

	public static String getIP(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	

	public static Date dateAddDay(Date date,int day){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH)+day);
		Date temp_date = c.getTime();
		return temp_date;
		
	}


	public static Date stringToDate(String dateStr, String formatStr) {
		DateFormat sdf = new SimpleDateFormat(formatStr);
		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static String getCNDate(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		
		return year+"年"+month+"月"+day+"日";
	}
	
}
